// pages/home/home.js
import { data, getHomeData} from '../../api/index.js'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [
      {
        icon: '/assets/images/home-icon1.png',
        name: '购买机器',
        path: 'buy'
      },
      {
        icon: '/assets/images/home-icon2.png',
        name: '收入明细',
        path: 'income'
      },
      {
        icon: '/assets/images/home-icon3.png',
        name: '申请培训',
        path: 'train'
      },
      {
        icon: '/assets/images/home-icon4.png',
        name: '推荐',
        path: 'recommend'
      }
    ],
    "target_machine_count": 19500, //
    "target_member_count": 150, //本区运营人数
    "current_machine_count": 0, //已有安装机器数
    "current_member_count": 0, //已有运营人数
    "rate": 0, //区域达成率
    currentRegion: '富阳区',
    identity: 'REGION',
    active: false
  },

  navigate: function(e) {
    const { currentTarget:{dataset:{path}}} = e
    const url = path ? `${path}/${path}`: 'home/home'

    console.log(url, 'home render url');

    wx.navigateTo({
      url: `/pages/${url}`,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getRegionData()
    this.renderSelectRegion()
    console.log(this.data, 888)
    this.initIdentity()
  },

  initIdentity: function() {
    const identity = wx.getStorageSync('identity')

    this.setData({
      identity
    })
  },

  renderSelectRegion: function() {
    const { applyParams, currentRegion} = this.data

    this.setData({
      currentRegion: applyParams ? applyParams.region : currentRegion
    })
  },

  //区域服务商数据查询
  getRegionData: function() {
    const identity = wx.getStorageSync('identity')
    return getHomeData({ identity })
          .then(regionInfo => {
            const { target_machine_count = 0, target_member_count = 0, current_machine_count = 0, current_member_count = 0, installed = 0, have=0, used=0, rate=0, active } = regionInfo.data
            console.log(regionInfo,999)

            this.setData({
              'target_machine_count': target_machine_count, 
              'target_member_count': target_member_count, 
              'current_machine_count': current_machine_count, 
              'current_member_count': current_member_count, 
              installed,
              rate,
              have,
              used,
              active
            })
          })
          .catch(e => {

            console.log(e, '区域服务商数据查询')
          })
  },

  onGotUserInfo: function(userInfo) {
    const { detail: {rawData}} = userInfo

    

  },

  onSelectRegion: function() {
    wx.navigateTo({
      url: '/pages/selectRegion/selectRegion',
    })
  }
})